<?php

// Presentation.php
// Michael Baker
// c. 2008
//
// rough user-interface module I wrote a long time ago
// not really part of the backend

if ( !defined( "IN_MDM" ) )
  exit;
  
require_once( "Database.php" );

function ui_get_timeframe_select()
{
  $arr1 = array(
  1 => 180,
  2 => 300,
  3 => 900,
  4 => 1800,
  5 => 3600,
  6 => 14400,
  7 => 43200,
  8 => 86400,
  9 => 172800
  );
  
  $arr2 = array();
  foreach ( array_keys( $arr1 ) as $id )
  {
    $seconds = $arr1[ $id ];
    
    if ( $seconds >= 3600 )
    {
      $hours = $seconds / 3600.0;
      $plur = get_plural( $hours );
      $arr2[ $id ] = $hours . " hour" . $plur;
    }
    else
    {
      $minutes = $seconds / 60.0;
      $plur = get_plural( $minutes );
      $arr2[ $id ] = $minutes . " minute" . $plur;
    }
  }
  
  return array( $arr1, $arr2 );
}
function get_subheader( $name )
{
  return "<span class=\"subheader\"><a href=\"" . WEB_ROOT . "\">" . SITE_NAME . "</a> - $name</span><br /><br />";
}

function get_section_table( $section_name, $header="Section Name" )
{
  global $dbase;
  
  $query = "SELECT `title`, `path`, `desc` FROM WebNav WHERE ";
  $query .= "`section`='$section_name' ORDER BY `order_id` ASC";
  $dbase->execute( $query );
  $returned = $dbase->numrows();
  
  $output = "";
  if ( !$returned )
    return $output;
  
  // we found some nav contents
  $output .= "<table class=\"nav\">";
  $output .= "<tr><th>$header</th></tr>";
  
  for ( $ctr = 0; $ctr < $returned; $ctr++ )
  {
    $row = $dbase->fetchone();
    
    $iTitle = $row[ 0 ];
    $iPath = WEB_ROOT . $row[ 1 ];
    $iDesc = $row[ 2 ];
    
    $output .= "<tr><td><span class=\"textgen\"><a href=\"$iPath\">$iTitle</a><br />$iDesc</span></td></tr>";
  }
  
  $output .= "</table>";
  
  return $output;
}

function top( $section_name, $header_info="" )
{
  /* Output the A3S page header.
  
  $section_name		The name of the section. */
  
  $script_path = WEB_ROOT;
  $title = SITE_NAME . " - $section_name";
  $css_path = $script_path . "/styles/blue.css";
  
  $time = "<span class=\"textblack\">" . date( "l, F j, Y @ h:i:s A" );
  $time .= "</span>";
  
  echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">
  <head>
  $header_info
  <title>$title</title>
  <link rel=\"stylesheet\" type=\"text/css\"
  href=\"$css_path\" />
  </head>
  <body class=\"bodygen\">
  <div>
  <br />$time
  <br /><br />";
}

function top_printable( $section_name )
{
  $script_path = WEB_ROOT;
  $title = "perfSONAR - $section_name";
  $css_path = $script_path . "/styles/blue.css";
  
  echo "<html>
  <head>
  <title>$title</title>
  <link rel=\"stylesheet\" type=\"text/css\"
  href=\"$css_path\" />
  </head>
  <body class=\"bodyprintable\"><div>
  <br /><br /><br />";
}

function output_error( $err )
{
  echo "<span class=\"texterror\">$err</span>";
}

function bot()
{
  // echo the a3s page footer
  echo "<br /><br /><span class=\"textgen\">Written by <strong>Michael Baker</strong><br />Tree diagrams generated by <a href=\"http://www.graphviz.org\">Graphviz</a></span>";
  echo "</div></body></html>";
  die;
}

function get_plural( $intval )
{
  return ($intval == 1) ? "" : "s";
}